package MyDisplayObjects
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	/** An object of this class presents text for a limited amount of time, may flash colors */
	public class PopUpTextField extends TextField
	{
		private var timer:Timer;
		private var color:uint, flashColor:uint;
		private var fontSize:Number;
		
		public function PopUpTextField(text:String, fontSize:Number, fontColor:uint, duration:int, flashColor:uint = 0x112358)
		{
			super();
			this.fontSize = fontSize;
			this.color = fontColor;
			this.flashColor = flashColor;
			this.selectable = false;
			this.mouseEnabled = false;
			this.defaultTextFormat = new TextFormat ("Arial", fontSize, fontColor);
			this.text = text;
			this.autoSize = "left";
			
			timer = new Timer(200, duration/1000*5);
			if (flashColor != 0x112358)
			{
				timer.addEventListener( TimerEvent.TIMER, flashText);
			}
			timer.addEventListener( TimerEvent.TIMER_COMPLETE, complete);
			timer.start();
		}
		
		/** Flash the text */
		private function flashText (event:TimerEvent):void
		{
			if (timer.currentCount % 2 == 0)
			{
				this.setTextFormat(new TextFormat("Arial", fontSize, color));
			} else
			{
				this.setTextFormat(new TextFormat("Arial", fontSize, flashColor));
			}
		}
		private function complete (event:TimerEvent):void
		{
			if (flashColor != 0x112358)
			{
				timer.removeEventListener( TimerEvent.TIMER, flashText);
			}
			timer.removeEventListener( TimerEvent.TIMER_COMPLETE, complete);
			timer.stop();
			this.dispatchEvent( new Event(Event.COMPLETE ));
		}
	}
}